<template>
  <div id="app">
    <div class="box">
      <div>
          <template>
            <el-select  v-model="selectedValue" clearable  placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
              <el-input class="l-input" style="width: 20%;margin-left: 10px;" v-model="search.keyWords" placeholder="关键字搜索"></el-input>
              
              <el-button style="margin-left: 10px;" type="primary" class="l-margin-l15" v-on:click="getList">查询</el-button>
          </template>
      </div>
  </div>
    <div class="box">
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="{ row }">
          <el-table :data="row.data" style="width: 100%">
            <el-table-column prop="code" label="权限编码"></el-table-column>
            <el-table-column prop="name" label="权限名称"></el-table-column>
            <el-table-column  label="操作" width="200" fixed="right" align="center">
              <template slot-scope="scope">
                  <el-button type="primary" size="mini"  plain
                      v-on:click="onEdit(scope.row,row.menuId)">编辑</el-button>
                      <el-button type="danger" size="mini"  plain
                      v-on:click="onDelete(scope.row.id)">删除</el-button>
              </template>
          </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column prop="menuCode" label="菜单编码"></el-table-column>
      <el-table-column prop="menuName" label="菜单名称"></el-table-column>

      <el-table-column label="操作" width="200" fixed="right" align="center">
        <template slot-scope="scope">
            <el-button type="success" size="mini"  plain
                v-on:click="onAdd(scope.row)">新增权限</el-button>
        </template>
    </el-table-column>
    </el-table>
  </div>
  <el-dialog :title="EditTitle" :close-on-click-modal="false" :visible.sync="dialogVisible" width="30%"
  custom-class="l-dialog">
  <div class="l-div-400">
      <el-form ref="form" :model="form" label-width="130px">
          <el-row>
              <el-col :span="24">
                  <el-form-item label="编码">
                      <el-input v-model="form.code" class="l-input-form" placeholder="权限编码"></el-input>
                  </el-form-item>
                  <el-form-item label="名称">
                      <el-input v-model="form.name" class="l-input-form" placeholder="权限名称"></el-input>
                  </el-form-item>
              </el-col>
          </el-row>
      </el-form>

  </div>
  <span slot="footer" class="dialog-footer">
      <el-button v-on:click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" v-on:click="onSave">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
import { GetPermissionListAll, SavePermission, DeletePermission,GetOption } from '@/service/systemapi';
import data from 'area-data';

export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      search: {
        keyWords: ""
      },
      form: {
          id : "",
          code : "",
          name : "",
          menuId:""
      },
      EditTitle:"",
      selectedValue: null, // 设置默认值为null
      options: [] // 初始化下拉列表为空数组
    };
  },
  watch: {
    selectedValue(newValue) {
      console.log('当前选中的值：', newValue);
    }
  },
  created() {
      this.InitData();
      this.InitOptions();
      this.currentPagePower=this.$route.name;
        console.log("assssssaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        
        this.userPower = localStorage.getItem("userPower");
        console.log(this.userPower)
  },
  methods:{
    InitOptions(){
      var that = this;
      GetOption().then(res => {
        that.options = res.data;
      })
    },
    InitData(){
      this.getList();
    },
    InitForm(){
      this.form.id = "";
      this.form.menuId = "";
      this.form.code = "";
      this.form.name = "";
    },
    getList() {
      this.openLoading();
      this.tableData = [];
      var that = this;
      GetPermissionListAll({"keyWords" : this.search.keyWords,
        "id" : this.selectedValue}).then(res => {
          console.log(res);
          if(res.success){
            that.tableData = res.data;
            that.$nextTick(function () {
              that.reflushPageFlag = true;
            });
          }else{
            this.$message.error(res.msg);
          }
          that.closeLoading();
      }).catch(err => {
        this.$message.error("数据请求异常");
        that.closeLoading();
      })
    },
    onEdit(data,menuId){
      this.InitForm();
      console.log(data)
      this.EditTitle = "修改";
      this.dialogVisible = true;
      this.form.code = data.code;
      this.form.name = data.name;
      this.form.id = data.id;
      this.form.menuId = menuId;
    },
    onDelete(id){
      this.$confirm("是否删除?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
      }).then(() => {
        var that = this;
        DeletePermission(id).then(res => {
          console.log(res);
          that.openLoading();
          if(res.success){
            this.$message.success("删除成功");
          }else{
            this.$message.error(res.msg);
          }
          that.closeLoading();
        }).catch(err => {
          this.$message.error("数据请求异常");
          that.closeLoading();
        })
      })     
    },
    onSave(){
      var that = this;
      console.log(this.form)
      SavePermission(this.form).then(res=>{
        console.log(res);
          that.openLoading();
          if(res.success){
            this.$message.success(res.data);
            that.dialogVisible = false;
            that.InitData();
          }else{
            this.$message.error(res.msg);
          }
          that.closeLoading();
        }).catch(err => {
          this.$message.error("数据请求异常");
          that.closeLoading();
        })
    },
    onAdd(data){
      this.InitForm();
      console.log(data)
      this.EditTitle = "新增";
      this.dialogVisible = true;
      this.form.menuId = data.menuId;
      this.form.id = "";
    }
  }
};
</script>